import { defineStore } from "pinia";
import { computed, ref } from "vue";
import { BasicControllerService, CommonUserInfoVO } from "@/api";

export const useUserStore = defineStore(
  "useUserStore",
  () => {
    const userInfo = ref<CommonUserInfoVO>({});
    const token = ref<string>("");
    const getToken = computed(() => token);

    async function getUserInfo() {
      await BasicControllerService.getUserInfoUsingGet().then((res) => {
        //存入数据
        // if (!res.data?.avatar) {
        //   res.data = {
        //     ...res.data,
        //     avatar: "",
        //   };
        // }
        userInfo.value = { ...res.data };
      });
    }

    function setToken(tokenValue: string) {
      token.value = tokenValue;
    }

    function logout() {
      userInfo.value = {};
      token.value = "";
    }

    return {
      token,
      userInfo,
      getUserInfo,
      getToken,
      setToken,
      logout,
    };
  },

  {
    //开启持久化
    persist: {
      key: "yx:shopping:user",
    },
  }
);
